<?php
session_start();
require_once( 'configs/config.php' );
require_once( 'lib/db.php' );
require_once( 'lib/functions.php' );
require_once(SMARTY_DIR . 'Smarty.class.php');
$smarty = new Smarty();

if( (isset( $_SESSION[ 'UID' ] ) && $_GET[ 'id' ] == $_SESSION[ 'UID' ] ) || $_SESSION[ 'Level' ] == $config[ 'Admin' ] )
{
	$smarty->assign('logged_in', 'true');
	$smarty->assign('admin', 'true');
	$smarty->assign('self', 'true');
}
else
{
	header( 'Location: login.php' );
	exit;
}

// Get list of tables from current database..
$dbPayments = $db->get_results('SELECT p.PaymentUID, CONCAT(u.FirstName, \' \', u.LastName) as Name, ABS(p.Amount) as Amount, SIGN(p.Amount) as Sign, p.Reason, DATE_FORMAT(p.Date, \'%M %e, %Y\') as Date FROM payments p, users u WHERE u.UserUID = p.UserUID ORDER BY p.Date DESC');

// Print out last query and results..
//$db->debug();

for($i = 0; $i < count($dbPayments); $i++)
{
	foreach($dbPayments[$i] as $key => $value)
	{
		if($key == 'Sign')
		{
			$payments[$i]['sign'] = $dbPayments[$i]->Sign >= 0 ? 'positive' : 'negative';
		}
		else
		{
			$payments[$i][strtolower($key)] = $value;
		}
	}
}

$smarty->assign('title', 'Display Payment');
$smarty->assign('payments', $payments);
$smarty->display('payments.tpl');

// get all assigned template vars
//$all_tpl_vars = $smarty->get_template_vars();
//print_r($all_tpl_vars);
?>